package UTS.model;

import java.sql.Statement;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Vincentius HMK
 */
public class Dosen {

    public static ArrayList<Dokumen> DAFTAR_DOKUMEN = new ArrayList<Dokumen>();

    public Dosen() {
    }

    public static void tambahDaftarDokumen(Dokumen dokumen) {
        DAFTAR_DOKUMEN.add(dokumen);
    }

    public static String bacaDaftarDokumen() {
        if (DAFTAR_DOKUMEN.isEmpty()) {
            return "kosong";
        } else {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_DOKUMEN.size(); i++) {
                hasil += DAFTAR_DOKUMEN.get(i).getNomor() + "\n";
            }
            return hasil;
        }
    }

    public static void simpanFileDokumen() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_DOKUMEN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Dokumen Kosong");
        } else {
            File outFile = new File("dokumen.dat");
            outStream = new FileOutputStream(outFile);
            ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
            outObjectStream.writeObject(DAFTAR_DOKUMEN);
            outStream.close();
        }
    }

    public static void bacaFileDokumen() throws FileNotFoundException, IOException, ClassNotFoundException {
        FileInputStream inStream = null;
        File inFile = new File("dokumen.dat");
        inStream = new FileInputStream(inFile);
        ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
        DAFTAR_DOKUMEN = (ArrayList<Dokumen>) inObjectStream.readObject();
    }

    public static void tambahTabelDokumen(Dokumen dokumen) throws SQLException {
//        PreparedStatement ps = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS085314016", "MHS085314016").prepareStatement("INSERT INTO DOKUMEN(nomor, perihal, asal, penerima, letak, tanggalDiterima, status) VALUES (?,?,?,?,?,?,?)");
        PreparedStatement ps = OraConnection.open("jdbc:oracle:thin:@localhost:1521:XE","SYSTEM", "MHS085314016").prepareStatement("INSERT INTO DOKUMEN(nomor, perihal, asal, penerima, letak, tanggalDiterima, status) VALUES (?,?,?,?,?,?,?)");
        ps.setString(1, dokumen.getNomor());
        ps.setString(2, dokumen.getPerihal());
        ps.setString(3, dokumen.getAsal());
        ps.setString(4, dokumen.getPenerima());
        ps.setString(5, dokumen.getLetak());
        ps.setDate(6, new java.sql.Date(dokumen.getTanggalDiterima().getTime()));
        ps.setString(7, dokumen.getStatusDokumen());

        ps.executeUpdate();
    }

    public static void simpanTabelDokumen() throws SQLException {
        if (DAFTAR_DOKUMEN.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Dokumen Kosong");
        } else {
//            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS085314016", "MHS085314016");
            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "SYSTEM", "MHS085314016");
            Statement statement = conn.createStatement();
            for (int i = 0; i < DAFTAR_DOKUMEN.size(); i++) {
                String sql = "INSERT INTO dokumen (nomor, perihal, asal, penerima, letak, tanggalDiterima, status) values ('" +
                        DAFTAR_DOKUMEN.get(i).getNomor() + "','" +
                        DAFTAR_DOKUMEN.get(i).getPerihal() + "','" +
                        DAFTAR_DOKUMEN.get(i).getAsal() + "','" +
                        DAFTAR_DOKUMEN.get(i).getPenerima() + "','" +
                        DAFTAR_DOKUMEN.get(i).getLetak() + "','" +
                        DAFTAR_DOKUMEN.get(i).getTanggalDiterima() + "','" +
                        DAFTAR_DOKUMEN.get(i).getStatusDokumen() + "')";
                statement.executeUpdate(sql);
            }
            conn.close();
        }
    }

    public static void bacaTabelDokumen() throws SQLException, Exception {
        DAFTAR_DOKUMEN = new ArrayList<Dokumen>();
//     Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS085314016", "MHS085314016");
        Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "SYSTEM", "MHS085314016");
        Statement statement = conn.createStatement();
        String sql = "select * from dokumen";
        ResultSet result = statement.executeQuery(sql);
        while(result.next()){
        Dokumen temp = new Dokumen();
        temp.setNomor(result.getString("nomor"));
        temp.setPerihal(result.getString("perihal"));
        temp.setAsal(result.getString("asal"));
        temp.setPenerima(result.getString("penerima"));
        temp.setLetak(result.getString("letak"));
        temp.setTanggalDiterima(result.getDate("tanggalDiterima"));
        temp.setStatusDokumen(result.getString("status"));
        DAFTAR_DOKUMEN.add(temp);
        }
        conn.close();
    }
}
